﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Elca.MvvmHelpers;

namespace ULSDeobfuscator {
    public class ComplexFilterViewModel : BaseViewModel {

        public ICommand OKCommand { get; private set; }
        public ICommand CancelCommand { get; private set; }
        public ICommand AddCommand { get; private set; }
        public ICommand EditCommand { get; private set; }
        public ICommand DeleteCommand { get; private set; }


        private ComplexFilter _currentFilter;

        public ComplexFilter CurrentFilter {
            get { return _currentFilter; }
            set {
                if (_currentFilter != value) {
                    _currentFilter = value;
                    OnPropertyChanged("CurrentFilter");
                }
            }
        }

        public ObservableCollection<ComplexFilter> Filters { get; set; }

        public double WindowHeight {
            get { return App.GlobalSettings.ComplexFiltersWindowHeight; }
            set {
                if (App.GlobalSettings.ComplexFiltersWindowHeight != value) {
                    App.GlobalSettings.ComplexFiltersWindowHeight = value;
                    OnPropertyChanged("WindowHeight");
                }
            }
        }

        public double WindowWidth {
            get { return App.GlobalSettings.ComplexFiltersWindowWidth; }
            set {
                if (App.GlobalSettings.ComplexFiltersWindowWidth != value) {
                    App.GlobalSettings.ComplexFiltersWindowWidth = value;
                    OnPropertyChanged("WindowWidth");
                }
            }
        }

        public double WindowLeft {
            get { return App.GlobalSettings.ComplexFiltersWindowLeft; }
            set {
                if (App.GlobalSettings.ComplexFiltersWindowLeft != value) {
                    App.GlobalSettings.ComplexFiltersWindowLeft = value;
                    OnPropertyChanged("WindowLeft");
                }
            }
        }

        public double WindowTop {
            get { return App.GlobalSettings.ComplexFiltersWindowTop; }
            set {
                if (App.GlobalSettings.ComplexFiltersWindowTop != value) {
                    App.GlobalSettings.ComplexFiltersWindowTop = value;
                    OnPropertyChanged("WindowTop");
                }
            }
        }

        public double ExpressionColumnWidth {
            get { return App.GlobalSettings.ComplexFiltersExpressionWidth; }
            set {
                if (App.GlobalSettings.ComplexFiltersExpressionWidth != value) {
                    App.GlobalSettings.ComplexFiltersExpressionWidth = value;
                    OnPropertyChanged("ExpressionColumnWidth");
                }
            }
        }

        public double RegexColumnWidth {
            get { return App.GlobalSettings.ComplexFiltersIsRegexWidth; }
            set {
                if (App.GlobalSettings.ComplexFiltersIsRegexWidth != value) {
                    App.GlobalSettings.ComplexFiltersIsRegexWidth = value;
                    OnPropertyChanged("RegexColumnWidth");
                }
            }
        }

        public double CaseSensibleColumnWidth {
            get { return App.GlobalSettings.ComplexFiltersIsCaseSensibleWidth; }
            set {
                if (App.GlobalSettings.ComplexFiltersIsCaseSensibleWidth != value) {
                    App.GlobalSettings.ComplexFiltersIsCaseSensibleWidth = value;
                    OnPropertyChanged("CaseSensibleColumnWidth");
                }
            }
        }

        public string TestMessage { get; set; }

        public ComplexFilterViewModel() {
            
            OKCommand = new ExecutableCommand(OnOK);
            CancelCommand = new ExecutableCommand(OnCancel);
            AddCommand = new ExecutableCommand(OnAddFilter);
            EditCommand = new ConditionalCommand(OnEditFilter, () => _currentFilter != null);
            DeleteCommand = new ConditionalCommand(OnDeleteFilter, () => _currentFilter != null);
        }

        private void OnClosing() {
            
        }
        private void OnOK() {
            OnClosing();
            RaiseCloseRequest(true);
        }

        private void OnCancel() {
            OnClosing();
            RaiseCloseRequest(false);
        }

        private void OnDeleteFilter() {

            Filters.Remove(CurrentFilter);
        }

        private void OnAddFilter() {

            ComplexFilter cf = new ComplexFilter {
                    IsCaseSensitive = true,
                    IsRegex = false
            };
            EditComplexFilterViewModel ecfvm = new EditComplexFilterViewModel();
            ecfvm.Filter = cf;
            ecfvm.Title = "Create new filtering expression";
            if (! String.IsNullOrEmpty(TestMessage)) {
                ecfvm.TestMessage = TestMessage;
            }
            IUIVisualizer viz = ServiceRegistry.Resolve<IUIVisualizer>();
        
            if (viz.ShowDialog(App.EditComplexFilter, ecfvm) == true) {
                
                Filters.Add(cf);
                CurrentFilter = cf;
            }
        }

        private void OnEditFilter() {

            ComplexFilter tempFilter = CurrentFilter.Clone();

            EditComplexFilterViewModel ecfvm = new EditComplexFilterViewModel();
            ecfvm.Filter = tempFilter;
            ecfvm.Title = "Edit the filtering expression";
            if (!String.IsNullOrEmpty(TestMessage)) {
                ecfvm.TestMessage = TestMessage;
            }

            IUIVisualizer viz = ServiceRegistry.Resolve<IUIVisualizer>();

            if (viz.ShowDialog(App.EditComplexFilter, ecfvm) == true) {

                tempFilter.CopyTo(CurrentFilter);
            }
        }
    }
}
